﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace GBPVRLibrary.MetadataLibrary
{
    public class TrailerSearch
    {
        /// <summary>
        /// Looks up trailers from a MovieTitle
        /// </summary>
        /// <param name="MovieTitle">the title of the movie to lookup the trailers for</param>
        /// <returns>a list of trailers found for the movie</returns>
        public static List<VideoTrailer> LookupTrailers(string MovieTitle)
        {
            MetadataLookup lookup = new MetadataLookup();
            List<MetadataLookupSearchResult> results = lookup.SearchMediaTypeSource("Video", "Apple", MovieTitle);

            if (results.Count > 0)
            {
                MetadataLookupSearchResult result = results[0];
                VideoTrailerCollection temp = lookup.LoadItem("Video", "Apple", result.Url, typeof(VideoTrailerCollection)) as VideoTrailerCollection;
                if(temp == null || temp.Trailers.Count == 0)
                    temp = (VideoTrailerCollection)lookup.LoadItem("Video", "Apple", result.Url + "hd", typeof(VideoTrailerCollection));
                if (temp == null)
                {
                    temp = new VideoTrailerCollection() { Trailers = new List<VideoTrailer>() };

                    VideoTrailerCollection high= (VideoTrailerCollection)lookup.LoadItem("Video", "Apple", result.Url + "high.html", typeof(VideoTrailerCollection));
                    VideoTrailerCollection large = (VideoTrailerCollection)lookup.LoadItem("Video", "Apple", result.Url + "large.html", typeof(VideoTrailerCollection));
                    VideoTrailerCollection medium = (VideoTrailerCollection)lookup.LoadItem("Video", "Apple", result.Url + "medium.html", typeof(VideoTrailerCollection));
                    VideoTrailerCollection small = (VideoTrailerCollection)lookup.LoadItem("Video", "Apple", result.Url + "small.html", typeof(VideoTrailerCollection));
                    VideoTrailerCollection low = (VideoTrailerCollection)lookup.LoadItem("Video", "Apple", result.Url + "low.html", typeof(VideoTrailerCollection));
                    if (high != null && high.Trailers.Count > 0)
                        temp.Trailers.AddRange(high.Trailers);
                    if (large != null && large.Trailers.Count > 0)
                        temp.Trailers.AddRange(large.Trailers);
                    if (medium != null && medium.Trailers.Count > 0)
                        temp.Trailers.AddRange(medium.Trailers);
                    if (small != null && small.Trailers.Count > 0)
                        temp.Trailers.AddRange(small.Trailers);
                    if (low != null && low.Trailers.Count > 0)
                        temp.Trailers.AddRange(low.Trailers);
                    
                }
                return temp == null ? new List<VideoTrailer>() : temp.Trailers;
            }

            return new List<VideoTrailer>();
        }
    }

    class VideoTrailerCollection
    {
        [MetadataAttribute]
        public List<VideoTrailer> Trailers { get; set; }
    }

    public class VideoTrailer
    {
        public enum TrailerQuailty
        {
            Unknown = 0,
            Low = 320,
            High = 640,
            TQ480p = 480,
            TQ720p = 720,
            TQ1080p = 1080
        }

        [MetadataAttribute]
        public TrailerQuailty Quality { get; set; }
        [MetadataAttribute]
        public string Url { get; set; }
        [MetadataAttribute]
        public string Name { get; set; }
        public string QualityToString
        {
            get
            {
                switch (Quality)
                {
                    case TrailerQuailty.TQ1080p:
                        return "1080p";
                    case TrailerQuailty.TQ480p:
                        if(Url.Contains("480p"))
                            return "480p";
                        return "Medium";
                    case TrailerQuailty.TQ720p:
                        return "720p";
                }
                return Quality.ToString();
            }
        }
    }
}
